<?php
function savemsg($title, $flag = 1, $content = array(), $userid = 0, $status = 1)
{
	if ($userid) {
		$userid = (int)$userid;
	} else {
		$userid = ugetcookie('userid');
	}
	$title = addslashes(stripslashes($title));
	$ip = getip();
	$ipport = getipport(); 
	$addtime = time();
	$strsql = "insert into `#yunyecms_msg`(userid,title,content,flag,status,ip,ipport,addtime) values('$userid','$title','$content',$flag,$status,'$ip','$ipport',$addtime)";
	$sql = query($strsql);
	return $sql; 
}

function islogin()
{
	$userid = ugetcookie("userid");
	$rnd = usafestr(ugetcookie("loginrnd"));
	if (empty($userid)) {
		messagebox('您还没有登录! ', url('member/member/login'));
	} else {
		$userid = usafestr(yunyecms_strdecode($userid));
		if (empty($userid) && !is_numeric($userid)) {
			messagebox('您还没有登录! ', url('member/member/login'));
		}
		$cntcheckone = getcount("select count(*) from `#yunyecms_member` where  `id`='$userid' and `rnd`='$rnd'");
		if (empty($cntcheckone)) {
			messagebox('您还没有登录! ', url('member/member/login'));
		}
	}
}

function checklogin()
{
	$userid = ugetcookie("userid");
	$rnd = usafestr(ugetcookie("loginrnd"));
	if (empty($userid)) {
		return false;
	} else {
		$userid = usafestr(yunyecms_strdecode($userid));
		if (empty($userid) && !is_numeric($userid)) {
			return false;
		}
		$member = getone("select *  from `#yunyecms_member` where  `id`='$userid' and `rnd`='$rnd'");
		if (empty($member)) {
			return false;
		} else {
			return $member;
		}
	}
}


//通过ID和栏目ID获取信息模型内容
function GetContentById($id, $catid)
{
	if (empty($id) || empty($catid)) {
		return false;
	}
	if (!is_numeric($id) && !is_numeric($catid)) {
		return false;
	}
	$modelid = getmodelid($catid);
	if ($modelid) {
		$curmodel = getmodel($modelid);
		if (!$curmodel) {
			return false;
		}
		$tablename = "m_" . $curmodel['tablename'];
		$retres = getone("select * from `#yunyecms_{$tablename}` where `id`= {$id}");
		if (empty($retres)) {
			return false;
		} else {
			return $retres;
		}
	} else {
		return false;
	}
}

function get_member_power($key, $subkey = "", $groupid)
{
	if (!$groupid || !is_numeric($groupid)) {
		return 0;
	} else {
		$strsql = "select * from `#yunyecms_membergroup` where `id`= {$groupid}";
		$cgroup = getone($strsql);
		if (empty($cgroup)) {
			$ret = 0;
		} else {
			$powersarr = array();
			$powers = yunyecms_strrange_decode($cgroup["powers"], $cgroup["salt"], $cgroup["range"]);
			if (!empty($powers)) {
				$powersarr = json_decode($powers, true);
			}
			$ret = yunyecms_getarrayvalue($powersarr, $key, $subkey);
		}
		return $ret;
	}
}

function get_mem_cat_power($catid, $type, $groupid)
{
	if (!$groupid || !is_numeric($groupid)) {
		return 0;
	} else {
		$strsql = "select * from `#yunyecms_membergroup` where `id`= {$groupid}";
		$cgroup = getone($strsql);
		if (empty($cgroup)) {
			$ret = 0;
		} else {
			$powercatarr = array();
			if (!empty($cgroup["powercat"])) {
				$powercat = yunyecms_strdecode($cgroup["powercat"]);
				$powercatarr = unserialize($powercat);
			}
			$ret = yunyecms_getarrayvalue($powercatarr, $catid, $type);
		}
		return $ret;
	}
}

function fetch_mem_cat_power($groupid)
{
	if (!$groupid || !is_numeric($groupid)) {
		return 0;
	}
	$strsql = "select * from `#yunyecms_membergroup` where `id`= {$groupid}";
	$cgroup = getone($strsql);
	if (empty($cgroup)) {
		$ret = 0;
	} else {
		$powercatarr = array();
		if (!empty($cgroup["powercat"])) {
			$powercat = yunyecms_strdecode($cgroup["powercat"]);
			$powercatarr = unserialize($powercat);
		}
		$ret = $powercatarr;
	}
	return $ret;
}

function get_mem_power_catid($powercat, $type = 'view')
{
	if (!empty($powercat)) {
		$catpwerid = array();
		foreach ($powercat as $key => $var) {
			if (!empty($var[$type])) {
				$catpwerid[] = $key;
			}
		}
		$return = !empty($catpwerid) ? $catpwerid : false;
		return $return;
	} else {
		return false;
	}
}
function get_visitor($db)
{
	if (empty($db)) $db = core::load_model('common_model');
	$visitor = $db->find("select  * from `#yunyecms_membergroup` where isvisitor=1 order by id ");
	if ($visitor) {
		return $visitor;
	} else {
		return false;
	}
}
function is_visitor($groupid, $db)
{
	if (empty($db)) $db = core::load_model('common_model');
	$visitor = $db->GetCount("select  count(*) from `#yunyecms_membergroup` where id={$groupid} and isvisitor=1 ");
	if ($visitor >= 1) {
		return true;
	} else {
		return false;
	}
}
function remove_style($content)
{
	$content = preg_replace("/<(\!.*?)>/si", "", $content); //过滤DOCTYPE   
	$content = preg_replace("/<(\/?html.*?)>/si", "", $content); //过滤html标签   
	$content = preg_replace("/<(\/?head.*?)>/si", "", $content); //过滤head标签   
	$content = preg_replace("/<(\/?meta.*?)>/si", "", $content); //过滤meta标签   
	$content = preg_replace("/<(\/?body.*?)>/si", "", $content); //过滤body标签   
	// $content = preg_replace('#font-family:[^;]+?;#i','',$content);
	$content = preg_replace('#line-height:[^;]+?;#i', '', $content);
	//$content = preg_replace('#font-size:[^;]+?;#i','',$content);
	$content = preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si", "", $content); //过滤script标签   
	$content = preg_replace("/<(\/?script.*?)>/si", "", $content); //过滤script标签  
	$content = preg_replace("/javascript/si", "Javascript", $content); //过滤script标签   
	$content = preg_replace("/vbscript/si", "Vbscript", $content); //过滤script标签   
	$content = preg_replace("/&#/si", "&＃", $content); //过滤script标签，如javAsCript:alert('aabb)
	// $content = preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式
	// $content = preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式
	return $content;
}

function getsubcatsel_partner($catid, $level, $pcatid = 0)
{
	$strspace = "";
	if ($level) {
		for ($i = 0; $i < $level; $i++) {
			$strspace = $strspace . "&nbsp;&nbsp;&nbsp;&nbsp;";
		}
		$strspace .= "|--";
	}
	if (isset($catid)) {
		$strsql = "select `id`,`title` from `#yunyecms_category` where `pid`= '{$catid}'  ";
		$subarr = getlist($strsql);
		if (!empty($subarr)) {
			foreach ($subarr as $key => $var) {
				$sid = $var["id"];
				if ($pcatid == $sid) {
					$retstr = '<option value="' . $sid . '" selected>' . $strspace . $var["title"] . '</option>';
				} else {
					$retstr = '<option value="' . $sid . '">' . $strspace . $var["title"] . '</option>';
				}
				echo $retstr;
				getsubcatsel_partner($sid, $level + 1, $pcatid);
			}
		}
	}
}
